package com.wx.mythread.thread;

public class MethodSynchronize implements Runnable {

    private static MethodSynchronize methodSynchronize = new MethodSynchronize();
    Object lock1 = new Object();
    Object lock2 = new Object();

    @Override
    public void run() {
        synchronized (lock1) {
            System.out.println("我是lock1对象锁代码块，" + Thread.currentThread().getName() + "开始执行");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我是lock1对象锁代码块，" + Thread.currentThread().getName() + "执行结束");
        }

        synchronized (lock1) {
            System.out.println("我是lock2对象锁代码块，" + Thread.currentThread().getName() + "开始执行");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我是lock2对象锁代码块，" + Thread.currentThread().getName() + "执行结束");
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(methodSynchronize);
        Thread thread2 = new Thread(methodSynchronize);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()){

        }
        System.out.println("game over");
    }
}
